package com.ywz.project.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ywz.common.ResultResp;
import com.ywz.common.StringUtils;
import com.ywz.project.base.system.entity.TSysPermission;
import com.ywz.project.base.system.service.TSysPermissionService;
import com.ywz.project.system.dto.req.PermissionPageReq;
import com.ywz.project.system.service.SysPermissionApiService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysPermissionApiServiceImpl implements SysPermissionApiService {
    @Resource
    private TSysPermissionService tSysPermissionService;

    @Override
    public ResultResp getPermissionPage(PermissionPageReq req) {
        LambdaQueryWrapper<TSysPermission> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TSysPermission::getIsDeleted, 0);
        Page<TSysPermission> page = new Page<>(req.getPageNum(), req.getPageSize());
        return ResultResp.success(tSysPermissionService.page(page, wrapper));
    }

    @Override
    public ResultResp getPermissionListByRoleId(String roleIds) {
        if(!StringUtils.isNumberAndSeparator(roleIds))
            return ResultResp.error("角色ids拼接格式错误");
        String[] split = roleIds.split(StringUtils.SEPARATOR);
        List<Integer> roleIdsList = Arrays.stream(split).map(Integer::parseInt).collect(Collectors.toList());
        List<TSysPermission> permissionListByRoleIds = tSysPermissionService.getPermissionListByRoleIds(roleIdsList);
        return ResultResp.success(permissionListByRoleIds);
    }
}
